import { query as queryUsers, queryCurrentMenu } from '../services/user';
import { message } from 'antd';
import { isResponerCode } from '../utils/utils';
export default {
  namespace: 'user',

  state: {
    currentUser: {
      name: '柏林 何',
      avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
      userid: '00000001',
      notifyCount: 0,
    },
    list: [],
    // currentUser: {},
    notifyCount: null,
    userResult: null,
    menuForCurrentUser: null,
  },

  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryUsers);
      if (!isResponerCode(response)) {
        return false
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchCurrent(_, { call, put }, callback) {
      const response = yield call(queryCurrentMenu);//queryCurrent
      console.log("res222222222222221", response)
      if (!isResponerCode(response)) {
        return false
      }
      yield put({
        type: 'saveCurrentUser',
        payload: {
          menuForCurrentUser: [],
        },
      });        
      yield put({
        type: 'saveCurrentUser',
        payload: {
          menuForCurrentUser: response.data,
          userResult: response.result
        },
      }); 
      if(callback)callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload,
        },
        // menuForCurrentUser: {
        //   ...state.menuForCurrentUser,
        //   notifyCount: action.payload,
        // },
      };
    },
  },
};
